package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.cache.GuiguCache;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;


    /**
     * 虚拟发货
     * /api/user/userInfo/savePaidRecord
     * @param
     * @return
     */
    @PostMapping("/userInfo/savePaidRecord")
    public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo){
        userInfoService.savePaidRecord(userPaidRecordVo);
        return Result.ok();
    }


    /**
     * 根据专辑id+用户ID获取用户已购买声音id列表
     * /api/user/userInfo/findUserPaidTrackList/{albumId}
     * @param albumId
     * @return
     */
    @GetMapping("/userInfo/findUserPaidTrackList/{albumId}")
    @GuiguLogin
    public Result<List<Long>> findUserPaidTrackList(@PathVariable Long albumId){
        Long userId = AuthContextHolder.getUserId();
        List<Long> list = userInfoService.findUserPaidTrackList(albumId,userId);
        return Result.ok();
    }

    /**
     * 判断用户是否购买过指定专辑
     * /api/user/userInfo/isPaidAlbum/{albumId}
     * @param albumId
     * @return
     */
    @GetMapping("/userInfo/isPaidAlbum/{albumId}")
    @GuiguLogin
    public Result<Boolean> isPaidAlbum(@PathVariable Long albumId){
        Long userId = AuthContextHolder.getUserId();
        Boolean bool = userInfoService.isPaidAlbum(albumId,userId);
        return Result.ok(bool);
    }

    /**
     * 获取用户声音列表付费情况
     * /api/user/userInfo/userIsPaidTrack/{userId}/{albumId}
     */
    @PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
    public Result<Map<Long,Integer>> userIsPaidTrack(@PathVariable Long userId,
                                                     @PathVariable Long albumId,
                                                     @RequestBody List<Long> needChackTrackIdList){
        Map<Long,Integer> map = userInfoService.userIsPaidTrack(userId,albumId,needChackTrackIdList);
        return Result.ok(map);
    }

    /**
     * 根据用户ID查询用户信息
     * /api/user/userInfo/getUserInfoVo/{userId}
     */
    @GuiguCache(prefix = "userInfoVo:")
    @GetMapping("/userInfo/getUserInfoVo/{userId}")
    public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId){
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
        return Result.ok(userInfoVo);
    }

}

